package com.LeetCode.why72;


/**
 * Created by Aqc on 2017/8/27.
 */
public class EditDistance {
    /**
     * 不能用贪心算法。比如：
     * "sea"
     * "eat"
     * 动态规划（Dynamic Programming, DP）
     */
    public static void main(String arg[]) {
        int a = minDistance("ab", "a");
        System.out.println(a);
    }

    private static int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        char[] wdchar1 = word1.toCharArray();
        char[] wdchar2 = word2.toCharArray();
        int[][] sum = new int[len1 + 1][len2 + 1];
        for (int i = 0; i <= len1; i++) {
            sum[i][0] = i;
        }
        for (int i = 0; i <= len2; i++) {
            sum[0][i] = i;
        }
        int i = 0;
        int j = 0;
        while (i < len1) {
            i++;
            while (j < len2) {
                j++;
                if (wdchar1[i - 1] == wdchar2[j - 1]) {
                    sum[i][j] = sum[i - 1][j - 1];
                } else {
                    sum[i][j] = myMin(sum[i - 1][j], sum[i][j - 1], sum[i - 1][j - 1]) + 1;
                }
            }
            j = 0;
        }
        return sum[len1][len2];
    }

    private static int myMin(int a, int b, int c) {
        int tmp = a < b ? a : b;
        return tmp < c ? tmp : c;
    }
}
